<template>
  <ContentWrap>
    <div :class="searchVisible ? 'flex flex-row flex-wrap mb-2' : 'flex flex-row flex-wrap'">
      <el-button type="primary" plain @click="openForm('create')" v-hasPermi="['cms:qlink:create']">
        <Icon icon="ep:plus" class="mr-5px" /> 新增
      </el-button>
      <el-button plain type="success" @click="searchVisible = !searchVisible">
        <Icon class="mr-5px" icon="ep:operation" />
        {{ searchVisible ? '关闭筛选' : '打开筛选' }}
      </el-button>
      <el-text type="info">
        <span class="ml-5px">{{ searchVisible ? '关闭以下筛选条件' : '点击打开筛选条件' }}</span>
      </el-text>
      <div class="flex-1"></div>
      <el-button @click="resetQuery">
        <Icon class="mr-5px" icon="ep:refresh" />
        刷新列表
      </el-button>
    </div>
    <el-form
      v-if="searchVisible"
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="主标题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入主标题"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="位置" prop="identify">
        <el-select
          v-model="queryParams.identify"
          placeholder="请选择位置"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in getStrDictOptions(DICT_TYPE.CMS_QLINK_IDENTIFY)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable class="!w-240px">
          <el-option
            v-for="dict in getIntDictOptions(DICT_TYPE.COMMON_STATUS)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间" prop="createTime">
        <el-date-picker
          v-model="queryParams.createTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
      <el-table-column label="主标题" align="center" prop="title" min-width="100px">
        <template #default="scope">
          <p>{{ scope.row.title }}</p>
          <p v-if="scope.row.subTitle">{{ scope.row.subTitle }}</p>
        </template>
      </el-table-column>
      <el-table-column label="位置" align="center" min-width="120px">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.CMS_QLINK_IDENTIFY" :value="scope.row.identify" />
        </template>
      </el-table-column>
      <el-table-column label="点击动作" align="center" min-width="120px">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.CMS_QLINK_ACTION" :value="scope.row.actionType" />
        </template>
      </el-table-column>
      <el-table-column label="图片" align="center" prop="picUrl" min-width="80px">
        <template #default="scope">
          <el-image
            fit="cover"
            :src="scope.row.picUrl"
            :preview-src-list="[scope.row.picUrl]"
            :initial-index="0"
            :preview-teleported="true"
            :hide-on-click-modal="true"
            class="flex-none w-50px h-50px ml-10px"
          />
        </template>
      </el-table-column>
      <!-- <el-table-column label="跳转链接" align="center" prop="linkUrl" min-width="150px" /> -->
      <el-table-column label="排序" align="center" prop="sort" min-width="60px" sortable />
      <el-table-column label="状态" align="center" prop="status" min-width="80px">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.COMMON_STATUS" :value="scope.row.status" />
        </template>
      </el-table-column>
      <!-- <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        :formatter="dateFormatter"
        width="180px"
      /> -->
      <el-table-column label="操作" align="center" fixed="right" width="120">
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="openForm('update', scope.row.id)"
            v-hasPermi="['cms:qlink:update']"
          >
            编辑
          </el-button>
          <el-button
            link
            type="danger"
            @click="handleDelete(scope.row.id)"
            v-hasPermi="['cms:qlink:delete']"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <QlinkForm ref="formRef" @success="getList" />
</template>

<script setup lang="ts">
import { DICT_TYPE, getStrDictOptions, getIntDictOptions } from '@/utils/dict'
import { dateFormatter } from '@/utils/formatTime'
import { QlinkApi, QlinkVO } from '@/api/cms/qlink'
import QlinkForm from './QlinkForm.vue'

/** 链接 列表 */
defineOptions({ name: 'Qlink' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const searchVisible = ref(false)

const loading = ref(true) // 列表的加载中
const list = ref<QlinkVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  title: undefined,
  identify: undefined,
  status: undefined,
  createTime: undefined
})
const queryFormRef = ref() // 搜索的表单

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await QlinkApi.getQlinkPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number | string) => {
  formRef.value.open(type, id)
}

/** 删除按钮操作 */
const handleDelete = async (id: number | string) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await QlinkApi.deleteQlink(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>
